﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;
using KPIS.GERP.GFMS.APP.App_UI.Commons;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;

namespace KPIS.GERP.GFMS.APP.App_UI.UserControls
{
    public partial class DropDownList_BankBranch : System.Web.UI.UserControl
    {
        public RadComboBox Child;
        Main main = new Main();
        IBLL_FIM_MAS_Banks bank = new B_FIM_MAS_Banks();
        IBLL_FIM_MAS_BankBranchs branch = new B_FIM_MAS_BankBranchs();

        protected void Page_Load(object sender, EventArgs e)
        {
            ddlBranchSeq.AutoPostBack = Child != null;
        }

        public RadComboBox GetComboBox()
        {
            RadComboBox ddl = ddlBranchSeq;
            return ddl;
        }

        public void LoadRootNodes(Nullable<int> nWidth)
        {
            if (nWidth != null)
                ddlBranchSeq.Width = Unit.Pixel(nWidth.Value);

            RadTreeView tree = (RadTreeView)ddlBranchSeq.Items[0].FindControl("treeBranchSeq");

            IList<M_FIM_MAS_Banks> infolist = bank.GetList();

            foreach (M_FIM_MAS_Banks info in infolist)
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.bank_name;
                node.Value = info.bank_seq.ToString();
                node.Category = "Level0";
                //node.Attributes.Add("category", "Level0");
                node.ExpandMode = TreeNodeExpandMode.ServerSideCallBack;
                tree.Nodes.Add(node);
            }
        }

        private void PopulateNodeOnDemand(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        {
            IList<M_FIM_MAS_BankBranchs> infolist = branch.GetList(e.Node.Value);
            //DataTable dtb = branch.SelectTreeView(e.Node.Value);

            foreach (M_FIM_MAS_BankBranchs info in infolist)
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.branch_name;
                node.Value = info.branch_seq.ToString();
                node.Category = "Level1";
                //if (Convert.ToInt32(row["ChildrenCount"]) > 0)
                //{
                //    node.ExpandMode = expandMode;
                //}
                e.Node.Nodes.Add(node);
            }

            e.Node.Expanded = true;
        }

        public void SetSelectedValue(string strValue, string strText)
        {
            if (strValue.Length > 0)
            {
                ddlBranchSeq.Items[0].Text = strText;
                txtBranchSeq.Text = strValue;
            }
            else
            {
                ddlBranchSeq.Text = "";
                txtBranchSeq.Text = "";
                txtBranchText.Text = "";
            }
        }

        public string GetSelectedValue()
        {
            string strValue = txtBranchSeq.Text;

            return strValue;
        }

        protected void treeBranchSeq_NodeExpand(object sender, RadTreeNodeEventArgs e)
        {
            PopulateNodeOnDemand(e, TreeNodeExpandMode.ServerSideCallBack);
        }

        protected void ddlBranchSeq_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (Child != null)
            {
                //if (ddlContactSeq.SelectedValue.Length > 0)
                //{
                //    switch (Child.ID)
                //    {
                //        case "ddlContactPersonSeq":
                //            App_UI.UserControls.DropDownList_ContactPerson uc = (App_UI.UserControls.DropDownList_ContactPerson)Child.Parent;
                //            uc.LoadControl(null, null, ddlContactSeq.SelectedValue);
                //            break;
                //    }
                //}
                ////else
                ////{
                ////    Child.DataSource = null;
                ////    Child.DataBind();
                ////}

                //Child.DataSource = null;
                //Child.DataBind();
            }
        }
    }
}